package cn.seocoo.platform.api.controller;

import cn.seocoo.platform.api.util.JSONResult;
import cn.seocoo.platform.base.dto.BaseCommonParentsDto;
import cn.seocoo.platform.base.dto.IndexDto;
import cn.seocoo.platform.base.service.IBaseVehicleTypeService;
import cn.seocoo.platform.common.constant.ClassContants;
import cn.seocoo.platform.common.utils.StringUtils;
import cn.seocoo.platform.information.service.IInformationService;
import cn.seocoo.platform.rotation.domain.RotationChart;
import cn.seocoo.platform.rotation.service.IRotationChartService;
import cn.seocoo.platform.user.domain.AppUser;
import cn.seocoo.platform.user.domain.AppUserDto;
import cn.seocoo.platform.user.service.IAppUserService;
import cn.seocoo.platform.validatecode.domain.Validatecode;
import cn.seocoo.platform.validatecode.service.ValidatecodeService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author WuJian
 * @Description //登录
 * @Date 15:23 2019/12/22
 * @Param
 * @return
 **/
@Api(value = "登录模块", description = "登录模块")
@RestController
@RequestMapping("/api/user")
public class ApiLoginController {

    @Autowired
    private IAppUserService appUserService;

    @Autowired
    private ValidatecodeService validatecodeService;

    @Autowired
    private IInformationService informationService;
    @Autowired
    private IBaseVehicleTypeService iBaseVehicleTypeService;
    @Autowired
    private IRotationChartService iRotationChartService;


    @PostMapping("/login")
    @ApiOperation("用户登录")
    @ResponseBody
    public JSONResult login(@RequestBody String str) {
        try {
            JSONObject json = JSON.parseObject(str);
            Validatecode validatecode = JSON.parseObject(json.toString(), Validatecode.class);
            AppUser appUserTemp = JSON.parseObject(json.toString(), AppUser.class);
            if (StringUtils.isEmpty(appUserTemp.getLoginName()) && StringUtils.isEmpty(appUserTemp.getWechatOpenId()) && StringUtils.isEmpty(appUserTemp.getQqOpenId())) {
                return JSONResult.errorMsg("登录账号不能为空");
            }
            if (StringUtils.isNotEmpty(appUserTemp.getLoginName())) {
                if (StringUtils.isEmpty(validatecode.getValidateCode())) {
                    return JSONResult.errorMsg("验证码不能为空");
                }else{
                    //校验手机号和验证码是否正确
                    validatecode.setPhone(appUserTemp.getLoginName());
                    boolean flag = validatecodeService.checkValidatecode(validatecode);
                    if (!flag) {
                        return JSONResult.errorMsg(ClassContants.VALIDATE_CODE_ERROR);
                    }
                }
            }

            Map<String, Object> map = appUserService.loginUser(appUserTemp);

            String loginType = map.get("loginType").toString();
            String otherType = map.get("otherType").toString();
            //未查询到用户信息并且没有第三方账号信息：登录失败
            if ("0".equals(loginType) && "0".equals(otherType)) {
                return JSONResult.errorMsg(ClassContants.LOGIN_ERROR);
            }
            //第三方账号未绑定：跳转绑定页面
            else if (!"0".equals(otherType)) {
                return JSONResult.ok(map);
            }
            AppUserDto appUserDto = (AppUserDto) map.get("appUserDto");
            //判断当前账号状态是否正常
            if (ClassContants.IS_FORBIDDEN.equals(appUserDto.getForbidden())) {
                return JSONResult.errorMsg("当前账号已被禁用");
            } else {
                return JSONResult.ok(map);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSONResult.errorMsg(JSONResult.ERROR_MSG);
    }


    /**
     * @return cn.seocoo.platform.common.core.domain.JSONResult
     * @Author WuJian
     * @Description //发送短信验证码
     * @Date 15:37 2019/12/23
     * @Param [loginName]
     **/
    @ApiOperation("发送手机验证码")
    @GetMapping("/sendValidateCode")
    public JSONResult sendValidateCode(String loginName) {
        try {
            int code;
            if (StringUtils.isBlank(loginName)) {
                return JSONResult.errorMsg("手机号不能为空");
            }
            code = validatecodeService.getValidatecode(loginName);
            if (code != 1) {
                return JSONResult.errorMsg("发送失败");
            }
            return JSONResult.ok(loginName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSONResult.errorMsg(JSONResult.ERROR_MSG);
    }


    @GetMapping("/index")
    public JSONResult index(Integer appUserId){
        IndexDto indexDto = new IndexDto();
        Integer count = 0;
        try {
            //未读消息数量
            if(appUserId == null){
                count  = informationService.getCountByUserId(appUserId);
            }

            //车型集合
            List<BaseCommonParentsDto>dtoList =  iBaseVehicleTypeService.selectBaseVehicleTypeForDto();
            //轮播图
            List<RotationChart> rotationChartList = iRotationChartService.selectRotationChartListForDto();
            indexDto.setCount(count);
            indexDto.setDtoList(dtoList);
            indexDto.setRotationChartList(rotationChartList);
            return  JSONResult.ok(indexDto);
        } catch (Exception e) {
            e.printStackTrace();
            return  JSONResult.errorMsg(JSONResult.ERROR_MSG);
        }


    }

}
